# Definition for a binary tree node.
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:
        line_sum = []
        line = [root]
        while line:
            temp = []
            line_sum.append(sum([node.val for node in line]))
            for node in line:
                if node.left:
                    temp.append(node.left)
                if node.right:
                    temp.append(node.right)
            line = temp
        line_sum.sort(reverse=True)
        return line_sum[k - 1] if -1 < k - 1 < len(line_sum) else -1

